package com.youaishujuhui.devops.web.controller.manage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.youaishujuhui.devops.web.domain.manage.SysResource;
import com.youaishujuhui.devops.web.domain.manage.SysRole;
import com.youaishujuhui.devops.web.service.manage.SysResourceService;
import com.youaishujuhui.devops.web.service.manage.SysRoleService;
import com.youaishujuhui.devops.web.vo.Page;
import com.youaishujuhui.devops.web.vo.Result;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  角色管理
 *
 * @author 陈沈钿
 * @create 2018-05-22 11:58
 **/
@Controller
@RequestMapping("/manage/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysResourceService sysResourceService;

    @RequestMapping("/page")
    public ModelAndView page(){
        return new ModelAndView("manage/role");

    }

    @RequestMapping("/query")
    @ResponseBody
    public Result<List<SysRole>> query(Integer limit,Integer offset,String sort,String order ){
        Map<String,Object> param = new HashMap<>();
        param.put("limit",limit);
        param.put("offset",offset);
        param.put("sort",sort);
        param.put("order",order);
        Result<List<SysRole>> result = new Result<>();
        int total = sysRoleService.total();

        List<SysRole> list =  sysRoleService.query(param);
        Page page = new Page(total,limit);
        result.setPage(page);
        result.setData(list);
        result.setCode(Result.SUCCESS);

        return result;
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result<String> add(String code, String name){
        Result<String> result = new Result<>();
        int count = sysRoleService.countByCode(code);
        if(count>0){
            result.setCode(Result.FAIL);
            result.setMsg(String.format("添加失败，code \"%s\" 已存在",code));
        }else{
        	SysRole po = new SysRole();
        	po.setCode(code);
        	po.setName(name);
            sysRoleService.insert(po);
            result.setCode(Result.SUCCESS);
            result.setMsg("添加成功");
        }

        return result;
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Result<SysRole> findById(Integer roleId){
        Result<SysRole> result = new Result<>();
        SysRole po = sysRoleService.findById(roleId);
        if(po==null){
            result.setCode(Result.FAIL);
            result.setMsg(String.format("记录已不存在！"));
        }else{
            result.setCode(Result.SUCCESS);
            result.setData(po);
        }

        return result;
    }

//
//    @RequestMapping("/del")
//    @ResponseBody
//    public Result<String> delete(Integer id){
//        Result<String> result = new Result<>();
//        sysRoleService.deleteById(id);
//        reportMenuService.deleteReportRoleMenuByRoleId(id);
//        result.setCode(Result.SUCCESS);
//        result.setMsg("删除成功！");
//        return result;
//    }
//
    @RequestMapping("/edit")
    @ResponseBody
    public Result<String> edit(SysRole po){
        Result<String> result = new Result<>();
        sysRoleService.update(po);
        result.setCode(Result.SUCCESS);
        result.setMsg("修改成功！");
        return result;
    }

    @RequestMapping("/resourceTree")
    @ResponseBody
    public Result<List<SysResource>> resourceTree(Integer roleId){
        Result<List<SysResource>> result = new Result<>();
        List<SysResource> resourcePOList = sysResourceService.resourceTree(roleId);
        result.setCode(Result.SUCCESS);
        result.setData(resourcePOList);
        return result;
    }

    @RequestMapping("/saveRoleResource")
    @ResponseBody
    public Result<String> saveRoleResource(Integer roleId, Integer[] resourceIds){
//        List<Map<String,Object>> list = new ArrayList<>();
//        for(int id : resourceIds){
//            Map<String,Object> map = new HashMap<>();
//            map.put("roleId", roleId.intValue());
//            map.put("resourceId", id);
//            list.add(map);
//        }
//        sysRoleService.deleteResourceByRoleId(roleId);
//        if(list.size()>0){
//            sysRoleService.saveRoleResource(list);
//        }
        
        SysRole role = sysRoleService.findById(roleId);
        
        sysRoleService.clearResources(role);
        
        List<SysResource> resources = role.getResources();
        if (resources == null) {
        	resources = new ArrayList<SysResource>();
        	role.setResources(resources);
        }
        
        for(int id : resourceIds) {
        	SysResource r = sysResourceService.findById(id);
        	if (r != null) {
        		resources.add(r);
        	}
        }
        
        sysRoleService.insertRelation(role);
        
        Result<String> result = new Result<>();
        result.setCode(Result.SUCCESS);
        result.setMsg("保存成功");
        return result;
    }

}
